package com.netx.generics.basic;
import com.netx.generics.time.Moment;
import com.netx.generics.time.Date;
import com.netx.generics.time.Time;
import java.text.DateFormat;


public class Property {

	private final String _name;
	private Object _value;

	public Property(String name, boolean value) {
		this(name, new Boolean(value));
	}

	public Property(String name, char value) {
		this(name, new Character(value));
	}

	public Property(String name, byte value) {
		this(name, new Byte(value));
	}

	public Property(String name, short value) {
		this(name, new Short(value));
	}

	public Property(String name, int value) {
		this(name, new Integer(value));
	}

	public Property(String name, long value) {
		this(name, new Long(value));
	}

	public Property(String name, float value) {
		this(name, new Float(value));
	}

	public Property(String name, double value) {
		this(name, new Double(value));
	}

	public Property(String name, Object value) {
		Checker.checkTextIdentifier(name, "name", true);
		_name = name;
		_value = value;
	}

	public String getName() {
		return _name;
	}

	public boolean booleanValue() {
		if(_value == null) {
			return false;
		}
		else if(_value instanceof Boolean) {
			return ((Boolean)_value).booleanValue();
		}
		else {
			return Boolean.valueOf(_value.toString()).booleanValue();
		}
	}
	
	public char charValue() {
		if(_value == null) {
			return '\0';
		}
		else {
			return _value.toString().charAt(0);
		}
	}

	public short shortValue() {
		if(_value == null) {
			return 0;
		}
		else if(_value instanceof Number) {
			return ((Number)_value).shortValue();
		}
		else {
			return new Short(_value.toString()).shortValue();
		}
	}

	public int intValue() {
		if(_value == null) {
			return 0;
		}
		else if(_value instanceof Number) {
			return ((Number)_value).intValue();
		}
		else {
			return new Integer(_value.toString()).intValue();
		}
	}

	public long longValue() {
		if(_value == null) {
			return 0;
		}
		else if(_value instanceof Number) {
			return ((Number)_value).longValue();
		}
		else {
			return new Long(_value.toString()).longValue();
		}
	}
	
	public float floatValue() {
		if(_value == null) {
			return 0;
		}
		else if(_value instanceof Number) {
			return ((Number)_value).floatValue();
		}
		else {
			return new Float(_value.toString()).floatValue();
		}
	}

	public double doubleValue() {
		if(_value == null) {
			return 0;
		}
		else if(_value instanceof Number) {
			return ((Number)_value).doubleValue();
		}
		else {
			return new Double(_value.toString()).doubleValue();
		}
	}

	public Object objectValue() {
		return _value;
	}

	public String stringValue() {
		if(_value == null) {
			return null;
		}
		else {
			return _value.toString();
		}
	}
	
	public Moment momentValue(DateFormat df) {
		Checker.checkNull(df, "df");
		if(_value == null) {
			return null;
		}
		else if(_value instanceof Moment) {
			return (Moment)_value;
		}
		else {
			return Moment.valueOf(_value.toString(), df);
		}
	}
	
	public Moment momentValue() {
		return momentValue(DateFormat.getDateTimeInstance());
	}

	public Date dateValue(DateFormat df) {
		Checker.checkNull(df, "df");
		if(_value == null) {
			return null;
		}
		else if(_value instanceof Date) {
			return (Date)_value;
		}
		else {
			return Date.valueOf(_value.toString(), df);
		}
	}
	
	public Date dateValue() {
		return dateValue(DateFormat.getDateInstance());
	}

	public Time timeValue(DateFormat df) {
		Checker.checkNull(df, "df");
		if(_value == null) {
			return null;
		}
		else if(_value instanceof Time) {
			return (Time)_value;
		}
		else {
			return Time.valueOf(_value.toString(), df);
		}
	}
	
	public Time timeValue() {
		return timeValue(DateFormat.getTimeInstance());
	}
	
	public String toString() {
		return getName()+": "+stringValue();
	}
}
